package controllers;

import beans.PageBean;
import beans.WebServiceBean;
import models.Article;
import models.Photo;
import models.Trend;
import play.libs.Codec;
import play.mvc.Controller;
import play.mvc.With;
import utils.Config;
import utils.ImageUtils;
import utils.SessionUtils;

import java.io.File;
import java.util.List;

/**
 * 文章页面
 * 
 * @author sirus
 */
@With(value = CheckLogin.class)
public class Articles extends Controller {

	// 文章编辑默认页面
	public static void index() {
		showArticle(1,0);
	}

	// 文章展示界面
	public static void showArticle(int page,int type) {
        long total = Article.count("is_verify = 0 and type!= 0 and is_delete = 0");
        page = page <= 0 ? 1 : page;
        PageBean pageBean = PageBean.getInstance(page, total, 10);
        if(type!=0){
            List<Article> articlelList = Article.find("is_verify != 0 and type!= 0 and is_delete = 0 order by createTime desc").
                    fetch(pageBean.getCurPage(), pageBean.getPerPage());
            render(articlelList, pageBean,type);

        }
        else{
            List<Article> articlelList = Article.find("is_verify = ? and is_delete = 0 and type!= 0 order by createTime desc",type).
                    fetch(pageBean.getCurPage(), pageBean.getPerPage());
            render(articlelList, pageBean,type);

        }

	}
    /**
     *@author 李宇星
     *审核文章接口
     **/
    public static void verifyArticle(String articleId,int type,int status)
    {
        Article article = Article.findById(articleId);
        article.is_verify = status;
        article.save();
        showArticle(0,type);
    }
	// 文本编辑器页面
	public static void editArticle(String articleId) {
		String articleID = Codec.UUID();
		session.put("articleID", articleID);
		if (articleId != null && !articleId.isEmpty()) {
			Article article = Article.findById(articleId);
			render(article);
		} else {
			render();
		}
	}

	// 保存文本的内容
	public static void saveArticle(Article article) {
		article.authorId = SessionUtils.getUserId();
		article.save();
		showArticle(1,0);
	}

	// 删除文本的内容
	public static void deleteArticle(String articleId, int curpage) {
		Article article = Article.findById(articleId);
		if (article != null) {
			article.remove();
		}
		showArticle(curpage,0);
	}
    // 链接富文本
    public static void upLoadPic(String action, File upfile) {
        // System.out.println("------------------");
        if (action.indexOf("config") >= 0) {
            String config = "{\"imageActionName\":\"uploadimage\",\"imageFieldName\":\"upfile\",\"imageMaxSize\":2048000,\"imageAllowFiles\":[\".png\",\".jpg\",\".jpeg\",\".gif\",\".bmp\"],\"imageCompressEnable\":true,\"imageCompressBorder\":1600,\"imageInsertAlign\":\"none\",\"imageUrlPrefix\":\"\",\"imagePathFormat\":\"/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}\"}";
            response.setHeader("Content-Type", "application/json;charset=UTF-8");
            renderText(config);
        } else if (action.indexOf("uploadimage") >= 0) {
            String id = Codec.UUID();
            File originFile = new File(Config.articleImgPath, id + ".jpg");
            ImageUtils.origin(upfile, originFile);

                StringBuffer sb = new StringBuffer();
                sb.append("{'url':'http://" + Config.fieldName2
                        + Config.articleImgPath + id + ".jpg");
                sb.append("','state':'SUCCESS','title':'" + id + "'}");
                renderText(sb);

        }
    }










}
